<?php
namespace app\center\controller;
use app\center\logic\IndexLogic;

/**
 * 个人中心api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/16
 * Time: 15:25
 */
class Index extends Base {

    public function __construct(){
        parent::__construct();
        $this->i_logic = new IndexLogic();
    }

    /**
     * 获取用户信息
     * @return \think\response\Json
     */
    public function ajax_get_user(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->i_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //微信信息
        $maps = [];
        $maps[] = ['openid','=',$u_detail['openid']];
        $v_detail = model('visitor')->get_detail($maps,'headimgurl');

        //获取待办事项未读数量
        $maps = [];
        $maps[] = ['deal_openid','=',$u_detail['openid']];
        $maps[] = ['status','=',2];
        $wm_no_read_num = model('matter')->get_count($maps,'id');

        //获取抄送给我未读数量
        $maps = [];
        $maps[] = ['copy_openid','=',$u_detail['openid']];
        $maps[] = ['status','=',1];
        $maps[] = ['is_read','=',0];
        $cm_no_read_num = model('copy')->get_count($maps,'id');

        //获取抄送批复未读数量
        $maps = [];
        $maps[] = ['from_openid','=',$u_detail['openid']];
        $maps[] = ['explain','<>',''];
        $maps[] = ['status','=',1];
        $maps[] = ['is_reply_read','=',0];
        $cr_no_read_num = model('copy')->get_count($maps,'id','sourceid,source');

        //获取积分排名
        $ir = new \app\common\controller\IntegralRank();
        $integral_ranking = $ir->get_user_integral_ranking($u_detail['total_integral']);

        $user_info = [];
        $user_info['username'] = $u_detail['username'];
        $user_info['role'] = $u_detail['role'];
        $user_info['headimgurl'] = $v_detail['headimgurl'];
        $user_info['user_power'] = $u_detail['user_power'];
        $user_info['total_integral'] = $u_detail['total_integral'];
        $user_info['integral_ranking'] = $integral_ranking;

        $data['user_info'] = $user_info;
        $data['wm_no_read_num'] = $wm_no_read_num;
        $data['cm_no_read_num'] = $cm_no_read_num;
        $data['cr_no_read_num'] = $cr_no_read_num;
        return json($this->i_logic->get_response(1,'OK',$data));
    }
}
